package se.recept.kollen.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author jocke
 */
@Entity
@Table(name = "IngredientCategory")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "IngredientCategory.findAll", query = "SELECT i FROM IngredientCategory i"),
    @NamedQuery(name = "IngredientCategory.findById", query = "SELECT i FROM IngredientCategory i WHERE i.id = :id"),
    @NamedQuery(name = IngredientCategory.FIND_BY_CUSTOMER, query = "SELECT i FROM IngredientCategory i WHERE i.customer = :customer AND i.mainCategory is NULL"),
    @NamedQuery(name = "IngredientCategory.findByName", query = "SELECT i FROM IngredientCategory i WHERE i.name = :name")})
public class IngredientCategory implements Serializable {

    public static final String FIND_BY_CUSTOMER = "IngredientCategory.findByCustomer";

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "NAME")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
    private List<Ingredient> ingredient;

    @JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Customer customer;

    @ManyToOne()
    @JoinColumn(name = "MAINCATEGORY_ID")
    private IngredientCategory mainCategory;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "mainCategory")
    private List<IngredientCategory> subCategories;

    public IngredientCategory() {
    }

    public IngredientCategory(Integer id) {
        this.id = id;
    }

    public IngredientCategory(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public List<Ingredient> getIngredient() {
        return ingredient;
    }

    public void setIngredient(List<Ingredient> ingredient) {
        this.ingredient = ingredient;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public IngredientCategory getMainCategory() {
        return mainCategory;
    }

    public void setMainCategory(IngredientCategory mainCategory) {
        this.mainCategory = mainCategory;
    }

    public List<IngredientCategory> getSubCategories() {
        return subCategories;
    }

    public void setSubCategories(List<IngredientCategory> subCategories) {
        this.subCategories = subCategories;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof IngredientCategory)) {
            return false;
        }
        IngredientCategory other = (IngredientCategory) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "se.recept.kollen.model.IngredientCategory[ id=" + id + " ]";
    }

}
